package com.mars.boss.weixin.web.contrller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.mars.boss.weixin.entity.WeixinMenu;
import com.mars.boss.weixin.exception.WeixinMenuBizException;
import com.mars.boss.weixin.service.WeixinMenuService;
import com.mars.boss.weixin.wx.model.Menu;
import com.mars.boss.weixin.wx.utils.WeixinContext;
import com.mars.boss.weixin.wx.utils.WeixinResultMsg;
import com.mars.boss.weixin.wx.utils.WeixinUtil;
import com.mars.common.utils.PropertyUtils;
import com.mars.web.common.support.BaseController;

@Controller
@RequestMapping("/weixin/menu")
public class WeixinMenuController extends BaseController {
	
	@Autowired
	private WeixinMenuService service ;
	
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main() {
		return viewName("main") ;
	}

	@RequestMapping(value="/form", method=RequestMethod.GET)
	public String form(Long id, Long pid, Model model) {
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		if(null != pid) {
			model.addAttribute("pid", pid) ;
		}
		return viewName("form") ;
	}
	
	@RequestMapping(value="/add", method=RequestMethod.POST)
	@ResponseBody
	public Object add(WeixinMenu entity) {
		try {
			this.service.insert(entity) ;
		} catch (WeixinMenuBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		
		return setResponseResultAddOk();
	}
	
	@RequestMapping(value="/delete/{ids}", method=RequestMethod.GET)
	@ResponseBody
	public Object delete(@PathVariable String ids) {
		try {
			this.service.deleteByIds(ids) ;
		} catch (WeixinMenuBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		return setResponseResultDeleteOk();
	}
	
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public Object update(WeixinMenu entity) {
		try {
			this.service.update(entity) ;
		} catch (WeixinMenuBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		return setResponseResultUpdateOk();
	}
	
	@RequestMapping(value="/get/{id}", method=RequestMethod.GET)
	@ResponseBody
	public WeixinMenu get(@PathVariable Long id) {
		return this.service.selectById(id) ;
	}
	
	@RequestMapping(value="/category", method=RequestMethod.GET)
	@ResponseBody
	public List<WeixinMenu> category() {
		return this.service.getMenuCategory() ;
	}
	
	@RequestMapping(value="/getChildren/{parent_id}", method=RequestMethod.GET)
	@ResponseBody
	public List<WeixinMenu> getChildren(@PathVariable Long parent_id) {
		return this.service.getMenuByParentId(parent_id) ;
	}
	
	@RequestMapping(value="/list", method=RequestMethod.GET)
	@ResponseBody
	public List<WeixinMenu> list() {
		return this.service.getAllMenus() ;
	}
	
	@RequestMapping(value="/push", method=RequestMethod.GET)
	@ResponseBody
	public Object push() {
		Map<String, List<Menu>> map = new HashMap<String, List<Menu>>() ;
		map.put("button", this.service.getAllMenusToWeixin()) ;
		
		WeixinResultMsg resultMsg = WeixinUtil.doPost(PropertyUtils.getValue("menu_url", WeixinContext.getAccessToken()), JSON.toJSONString(map)) ;
		if(resultMsg.getErrcode() > 0) {
			logger.error("添加微信菜单失败：{}", JSON.toJSONString(resultMsg));
			return setResponseResultFailed("添加微信菜单失败：" + resultMsg.getErrmsg()) ;
		} else {
			return setResponseResultOk("发送到微信服务器成功.") ;
		}
		
		
	}
	
	@RequestMapping(value="/clearCache", method=RequestMethod.GET)
	@ResponseBody
	public Object clearCache() {
		this.service.clearCache() ;
		return setResponseResultOk() ;
	}
	
}
